﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ESRI.ArcGIS.Client;
using Eaglight.Extentions;
using System.Windows.Interactivity;
using ESRI.ArcGIS.Client.Symbols;
using Eaglight;
using ESRI.ArcGIS.Client.Geometry;

namespace Eaglight.Behaviors
{
    public class ZoomOutBehavior : Behavior<Map>
    {
        private Envelope getZoomOutExtent(Envelope currentExtent, Envelope zoomBoxExtent)
        {
            MapPoint zoomBoxCenter = zoomBoxExtent.GetCenter();

            double whRatioCurrent = currentExtent.Width / currentExtent.Height;
            double whRatioZoomBox = zoomBoxExtent.Width / zoomBoxExtent.Height;

            Envelope newEnv = null;

            if (whRatioZoomBox > whRatioCurrent)
            // use width
            {
                double mapWidthPixels = this.AssociatedObject.Width;
                double multiplier = currentExtent.Width / zoomBoxExtent.Width;
                double newWidthMapUnits = currentExtent.Width * multiplier;
                newEnv = new Envelope(new MapPoint(zoomBoxCenter.X - (newWidthMapUnits / 2), zoomBoxCenter.Y),
                                               new MapPoint(zoomBoxCenter.X + (newWidthMapUnits / 2), zoomBoxCenter.Y));
            }
            else
            // use height
            {
                double mapHeightPixels = this.AssociatedObject.Height;
                double multiplier = currentExtent.Height / zoomBoxExtent.Height;
                double newHeightMapUnits = currentExtent.Height * multiplier;
                newEnv = new Envelope(new MapPoint(zoomBoxCenter.X, zoomBoxCenter.Y - (newHeightMapUnits / 2)),
                                               new MapPoint(zoomBoxCenter.X, zoomBoxCenter.Y + (newHeightMapUnits / 2)));
            }

            return newEnv;
        }
        
        protected override void OnAttached()
        {
            base.OnAttached();
            AssociatedObject.Cursor = Cursors.Stylus;
            Draw draw = this.AssociatedObject.GetDrawObject();
            draw.DrawMode = DrawMode.Rectangle;
            draw.LineSymbol = new SimpleLineSymbol();
            draw.DrawComplete += new EventHandler<DrawEventArgs>(draw_DrawComplete);
            draw.Map = this.AssociatedObject;
            draw.IsEnabled = true;
        }

        void draw_DrawComplete(object sender, DrawEventArgs e)
        {
            if (AssociatedObject != null)
            {
                this.AssociatedObject.ZoomTo(getZoomOutExtent(AssociatedObject.Extent, e.Geometry.Extent));
            }
        }

        protected override void OnDetaching()
        {
            AssociatedObject.Cursor = Cursors.Arrow;
            Draw draw = this.AssociatedObject.GetDrawObject();
            draw.IsEnabled = false;
        }


    }
}
